﻿using System;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;

using Jungle.Code.Utils;

namespace Serenity.Modules.fckeditor.editor.filemanager.ImageUploader
{
    public partial class ImageUploader : System.Web.UI.Page, IPostBackEventHandler
    {
        public string basePath = "/UserFiles/Images/Content/";
        protected override void OnInit(EventArgs e)
        {
            if(!IsPostBack)
                LoadTree();
            base.OnInit(e);
        }
        // --------------------------------------------------------------
        private void LoadTree()
        {
            DirectoryInfo DirInfo = new DirectoryInfo(Server.MapPath(basePath + Request["dir"]));
            DirectoryInfo[] subDirs = DirInfo.GetDirectories();
            FileInfo[] Files = DirInfo.GetFiles();

            if (Request["dir"] != null)
            {
                string path = "?dir=";
                if (Request["dir"].IndexOf("\\") == -1) path = "";
                else
                {
                    path += Request["dir"].Substring(0,Request["dir"].LastIndexOf("\\"));
                }
                HyperLink hp = new HyperLink();
                hp.NavigateUrl = UrlHelper.GetSiteUrl() + "/Modules/fckeditor/editor/filemanager/ImageUploader/ImageUploader.aspx" + path;

                hp.Text = "<img align=\"absmiddle\" src=\"" + UrlHelper.GetSiteUrl() + "/Admin/img/cat_up.gif\">&nbsp;...<br/>";

                ph_Catalog.Controls.Add(hp);            
            }

            foreach (DirectoryInfo d in subDirs)
            {
                HyperLink hp = new HyperLink();
                hp.NavigateUrl = UrlHelper.GetSiteUrl() + "/Modules/fckeditor/editor/filemanager/ImageUploader/ImageUploader.aspx?dir=" + d.FullName.Substring(d.FullName.IndexOf("Content") + "Content".Length + 1);

                hp.Text = "<img align=\"absmiddle\" src=\"" + UrlHelper.GetSiteUrl() + "/Admin/img/cat_folder.gif\">&nbsp;" + Path.GetFileName(d.FullName);

                ph_Catalog.Controls.Add(hp);
                ph_Catalog.Controls.Add(DeleteButton(false, d.FullName));

                Label lb0 = new Label();
                lb0.Text = "<br/>";
                ph_Catalog.Controls.Add(lb0);


            }

            foreach (FileInfo f in Files)
            {
                Label lb2 = new Label();
                lb2.Text = "<span style=\"cursor: pointer;\" onclick=\"SetItem('" + UrlHelper.GetSiteUrl() + basePath + Request["dir"] + Path.GetFileName(f.FullName) + "');\"><img align=\"absmiddle\" src=\"" + UrlHelper.GetSiteUrl() + "/Admin/img/cat_img.gif\">&nbsp;" + Path.GetFileName(f.FullName) + "</span>";
                ph_Catalog.Controls.Add(lb2);
                ph_Catalog.Controls.Add(DeleteButton(true, f.FullName));

                Label lb3 = new Label();
                lb3.Text = "<br/>";
                ph_Catalog.Controls.Add(lb3);
            }        
        }
        // --------------------------------------------------------------
        public void CreateFolder(object sender, EventArgs e)
        {
            Directory.CreateDirectory(Server.MapPath(basePath + Request["dir"] + "/" + tb_NewFolderName.Text));
            LoadTree();
        }
        // --------------------------------------------------------------
        public void Upload(object sender, EventArgs e)
        {
            if (fu_File.HasFile)
            {
                fu_File.SaveAs(Server.MapPath(basePath + Request["dir"] + "\\" + fu_File.FileName));
                LoadTree();
            }
        }
        // --------------------------------------------------------------
        private Image DeleteButton(bool type, string delPath)
        {
            ClientScriptManager cs = Page.ClientScript;
            PostBackOptions opt = new PostBackOptions(this);
            opt.ActionUrl = Context.Request.RawUrl;

            Image btn = new Image();
            switch (type)
            { 
                case false:
                    opt.Argument = "delDir," + delPath;
                    break;

                case true:
                    opt.Argument = "delFile," + delPath;
                    break;
            }
            btn.Attributes.Add("onclick", "if(confirm(\"Вы действительно хотите удалить?\"))" + cs.GetPostBackEventReference(opt));
            btn.ImageUrl = UrlHelper.GetSiteUrl() + "/Admin/img/btn_delete.jpg";
            btn.Attributes.Add("title", "Удалить");
            btn.Attributes.Add("align", "absmiddle");
            btn.Attributes.Add("style", "cursor: pointer; margin-left: 10px;");

            return btn;
        }
        //--------------------------------------------------------------
        public void RaisePostBackEvent(string eventArgument)
        {
            string[] prms = eventArgument.Split(',');

            if (prms[0] == "delDir") // удаление каталога
            {
                Directory.Delete(prms[1], true);
                LoadTree();
            }

            if (prms[0] == "delFile") // удаление файла
            {
                File.Delete(prms[1]);
                LoadTree();
            }
        }
    }
}
